package com.hwadee.teachingprocessmanagementsystem.controller.student;

import com.hwadee.teachingprocessmanagementsystem.dataform.StudentCourseChangeForm;
import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentCourseChangeDto;
import com.hwadee.teachingprocessmanagementsystem.entity.CourseChangeRequest;
import com.hwadee.teachingprocessmanagementsystem.entity.Student;
import com.hwadee.teachingprocessmanagementsystem.entity.Teacher;
import com.hwadee.teachingprocessmanagementsystem.service.base.CourseChangeRequestService;
import com.hwadee.teachingprocessmanagementsystem.service.student.StudentService;
import com.hwadee.teachingprocessmanagementsystem.service.teacher.TeacherService;
import com.hwadee.teachingprocessmanagementsystem.util.result.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description: 学生调课管理控制器，负责学生的调课请求
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.controller.student
 * @Project: TeachingProcessManagementSystem
 * @name: CourseChangeController
 * @Date: 2024/7/12-9:06
 * @Filename: CourseChangeController
 */
@RestController("StudentCourseChangeController")
@RequestMapping("student/course-change")
public class CourseChangeController {
    @Autowired
    private CourseChangeRequestService courseChangeRequestService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    /**
     * 调课申请接口
     * @return
     */
    @GetMapping("application")
    public DataResult getApplication(@RequestParam("student-number") String studentNumber){
        if (studentNumber.length() == 10) {
            Student student = new Student();
            student.setStudentNumber(studentNumber);
            StudentCourseChangeDto studentCourseChangeDto = this.studentService.getStudentCourseChangeDto(student);
            return DataResult.successByData(studentCourseChangeDto);
        } else {
            Teacher teacher = new Teacher();
            teacher.setTeacherId(Integer.valueOf(studentNumber));
            StudentCourseChangeDto studentCourseChangeDto = this.teacherService.getStudentCourseChangeDto(teacher);
            return DataResult.successByData(studentCourseChangeDto);
        }

    }

    @PostMapping("application")
    public DataResult postApplication(@RequestBody StudentCourseChangeForm form, @RequestParam("student-number") String studentNumber){
        CourseChangeRequest courseChangeRequest = new CourseChangeRequest();
        String courseTeacherId = form.getCourseId();
        String[] split = courseTeacherId.split("-");
        String courseId = split[0];
        String teacherId = split[1];
        courseChangeRequest.setCourseId(courseId);
        courseChangeRequest.setTeacherId(Integer.valueOf(teacherId));

        String originalDate = form.getOriginalDate();
        courseChangeRequest.setOriginalDate(Integer.parseInt(String.valueOf(Long.parseLong(originalDate)/1000)));

        String rescheduleDate = form.getRescheduleDate();
        courseChangeRequest.setNewDate(Integer.parseInt(String.valueOf(Long.parseLong(rescheduleDate)/1000)));

        String originalWeek = form.getOriginalWeek();
        courseChangeRequest.setOriginalWeek(Integer.parseInt(originalWeek));

        String rescheduleWeek = form.getRescheduleWeek();
        courseChangeRequest.setNewWeek(Integer.parseInt(rescheduleWeek));

        String originalTime = form.getOriginalTime();
        courseChangeRequest.setOriginalTime(originalTime);

        String rescheduleTime = form.getRescheduleTime();
        courseChangeRequest.setNewTime(rescheduleTime);

        courseChangeRequest.setStatus("审核中");

        courseChangeRequest.setStudentNumber(studentNumber);

        String reason = form.getReason();
        courseChangeRequest.setReason(reason);

        Date date = new Date();
        long timestamp = date.getTime() / 1000;
        courseChangeRequest.setTimestamp(timestamp);

        this.courseChangeRequestService.insert(courseChangeRequest);
        return DataResult.succ();
    }
}
